//
//  VisitorView.swift
//  weibo
//
//  Created by 孙新 on 16/4/16.
//  Copyright © 2016年 孙新. All rights reserved.
//

import UIKit

//定义代理
@objc protocol VisitorViewDelegate: NSObjectProtocol{
    func loginClickButton()
    func registorClickButton()
    
    //如果不加optional默认的是必选方法 必须实现  反之亦然
    
}

class VisitorView: UIView {
    
    //遵守协议代理
    weak var delegate : VisitorViewDelegate?
    
    //监听登录按钮的方法
    @objc private func loginClickButton(){
        
        //点击登录按钮按钮的颜色就变为orange
        loginButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        registerButton.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
      
        delegate?.loginClickButton()
    }
    //监听注册按钮的方法
    @objc private func registorClickButton(){
        
        //点击注册按钮让注册按钮的字体颜色变为orange 登录按钮的变为darkGray
        registerButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
      
        loginButton.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
       
        delegate?.registorClickButton()
    }
    

   //调用init 初始化显示的内容
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.frame = UIScreen.mainScreen().bounds
        setupUI()
      
    }

    //下面这个方法是体现swift的严谨性 aDecoder是在用xib的方式创建走的方法 只要创建不是用代码的形式就是用xib的形式 为了让阅读代码的人直观的区分 所以两个方法都要写 如果用代码的形式调用Xib创建好的控件就咬在aDecoder里面调用一下super.init  要不然要对控件进行设置是无效的 因为他不走这个方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        //当切换控制器调用的方法 让转图根据不同控制器决定是否要显示 并修改label上的文字
    func setVisitorInfo(imgName: String?,message: String?){
        if let img = imgName,text = message{
            iconView.image = UIImage(named: img)
            messageLabel.text = text
        }else{
            startAnim()
        }
    }
   
    
    private func startAnim(){
        
        //利用核心动画让图片转
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //显示iconView
        circleView.hidden = false
        
        //转一圈
        anim.toValue = 2 * M_PI
        //重复次数
        anim.repeatCount = MAXFLOAT
        //时间间隔
        anim.duration = 20
        //切换控制器不移除动画效果
        anim.removedOnCompletion = false
        
        //添加到iconView上
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    //设置显示的内容
    private func setupUI(){
        
        //设置view的背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        //添加控件 注意层次！！！
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //给控件进行约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: -10))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -20))
        //添加圆圈
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -15))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 15))
        
        //添加按钮约束
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        //登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier:1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        //透明View
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
    }
    //MARK － 懒加载控件
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //曾加circleView默认隐藏
    private lazy var circleView:UIImageView = {
        let image = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        image.hidden = true
        return image
    }()
    private lazy var messageLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 12)
        label.text = "滚滚长江东逝水，浪花淘尽多少英雄泪！！！！！"
        label.textAlignment = .Center
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = 230
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton:UIButton = {
        
        //默认按钮的字体颜色
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        //给按钮添加一个点击事件
        button.addTarget(self, action: "registorClickButton", forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState:.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        return button
    }()
    
    //登录 按钮
    private lazy var loginButton:UIButton = {
        //默认字体的颜色
       let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        //给登录按钮添加一个点击事件
        button.addTarget(self, action: "loginClickButton", forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        return button
    }()
 
    
    //半透明背景
    private lazy var maskIconView:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))


   






}
